﻿using ABlog.Domain.Entity.Foundation;
using ABlog.Domain.Repository;
using EasyCommonCore.Common;
using EasyCommonCore.Extension;

namespace ABlog.Domain.Entity
{
    /// <summary>
    /// 用户
    /// </summary>
    public partial class UserEntity : FullAudited
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 是否后台管理员
        /// </summary>
        public bool IsAdmin { get; set; }

        /// <summary>
        /// 岗位Guid
        /// </summary>
        public string PostGuid { get; set; }

    }

    /// <summary>
    /// 用户
    /// </summary>
    public partial class UserEntity
    {
        public UserEntity() { }

        /// <summary>
        /// 前台注册新用户
        /// </summary>
        /// <param name="userName">用户名称</param>
        /// <param name="password">用户密码</param>
        public UserEntity(string userName, string password)
        {
            UserName = userName;
            Password = password.ToMd5().ToSha1();
        }

        #region 设置为后台账户
        /// <summary>
        /// 设置为后台账户
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="operateUserId">操作用户id</param>
        /// <param name="postGuid">岗位Guid</param>
        public void SetAdmin(string userId, string operateUserId, string postGuid)
        {
            var userInfo = _userRepository.FindFirstOrDefault(x => x.ID == userId);
            AssertExtend.IsNotNullByClass(userInfo, "用户不存在");
            userInfo.IsAdmin = true;
            userInfo.PostGuid = postGuid;
            ChangeInformation(operateUserId);
        }
        #endregion

        #region 取消后台账户
        /// <summary>
        /// 设置为后台账户
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="operateUserId">操作用户id</param>
        public void CancelAdmin(string userId, string operateUserId)
        {
            var userInfo = _userRepository.FindFirstOrDefault(x => x.ID == userId);
            AssertExtend.IsNotNullByClass(userInfo, "用户不存在");
            userInfo.IsAdmin = false;
            ChangeInformation(operateUserId);
        }

        #endregion
    }
}
